import { UPDATE_STATE, UPDATE_ALL_STATE, UPDATE_GOODS_COUNT, INIT_GOODS } from '../constant'
import { initCartAPI } from '../../api/cart.js'

// payload { id, value }
export const updateStateAction = payload => ({
  type: UPDATE_STATE,
  payload
})

export const updateAllStateAction = boo => ({
  type: UPDATE_ALL_STATE,
  payload: boo
})

// payload { id, count }
export const updateCountAction = payload => ({
  type: UPDATE_GOODS_COUNT,
  payload
})

// 获取商品列表数据的 action
export const initGoodsAction = () => {
  // 返回一个函数，形参是 dispatch
  return async dispatch => {
    const { data: res } = await initCartAPI()
    dispatch({
      type: INIT_GOODS,
      payload: res.list
    })
  }
}
